package gui;


import javax.swing.*;
//import javax.swing.text.*;

import document.Manuscript;
//import document.ManuscriptCollection;
	 
import java.awt.*;              //for layout managers and more
import java.awt.event.*;        //for action events
	 
//import java.net.URL;
//import java.io.IOException;
	
	
	public class TextSamplerDemo extends JPanel
	                             /*implements ActionListener*/ {
	    private String newline = "\n";
	    protected static final String titleFieldString = "Title";
	    protected static final String authorFieldString = "Author";
	    protected static final String ftfString = "Date";
	    protected static final String buttonString = "JButton";
	 
	    protected JLabel actionLabel;
	 
	    public TextSamplerDemo(final Manuscript m) {
	        setLayout(new BorderLayout());
	 
	        //Create a regular text field.
	        JTextField titleField = new JTextField(40);
	        titleField.setText("Manuscript.getTitle()");
	 
	        //Create a password field.
	        JTextField authorField = new JTextField(40);
	        authorField.setText("Manuscript.getAuthor()");

	 
	        //Create a formatted text field.
	        JFormattedTextField ftf = new JFormattedTextField(
	                java.util.Calendar.getInstance().getTime());

	        
	        String the_text = m.getText();
	 
	        //Create some labels for the fields.
	        JLabel titleFieldLabel = new JLabel(titleFieldString + ": ");
	        titleFieldLabel.setLabelFor(titleField);
	        JLabel authorFieldLabel = new JLabel(authorFieldString + ": ");
	        authorFieldLabel.setLabelFor(authorField);
	        JLabel ftfLabel = new JLabel(ftfString + ": ");
	        ftfLabel.setLabelFor(ftf);
	 
	        //Create a label to put messages during an action event.
	        actionLabel = new JLabel();
	        actionLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
	 
	        //Lay out the text controls and the labels.
	        JPanel textControlsPane = new JPanel();
	        GridBagLayout gridbag = new GridBagLayout();
	        GridBagConstraints c = new GridBagConstraints();
	 
	        textControlsPane.setLayout(gridbag);
	 
	        JLabel[] labels = {titleFieldLabel, authorFieldLabel, ftfLabel};
	        JTextField[] textFields = {titleField, authorField, ftf};
	        addLabelTextRows(labels, textFields, gridbag, textControlsPane);
	 
	        c.gridwidth = GridBagConstraints.REMAINDER; //last
	        c.anchor = GridBagConstraints.WEST;
	        c.weightx = 1.0;
	        textControlsPane.add(actionLabel, c);
	        textControlsPane.setBorder(
	                BorderFactory.createCompoundBorder(
	                                BorderFactory.createTitledBorder("Text Fields"),
	                                BorderFactory.createEmptyBorder(5,5,5,5)));
	 
	        //Create a text area.
	        JTextArea textArea = new JTextArea(
	                m.getTitle()
	        );
	        textArea.setEditable(false);
	        textArea.setFont(new Font("Serif", Font.PLAIN, 14));
	        textArea.setLineWrap(true);
	        textArea.setWrapStyleWord(true);
	        JScrollPane areaScrollPane = new JScrollPane(textArea);
	        areaScrollPane.setVerticalScrollBarPolicy(
	                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	        areaScrollPane.setPreferredSize(new Dimension(500, 500));
	        areaScrollPane.setBorder(
	            BorderFactory.createCompoundBorder(
	                BorderFactory.createCompoundBorder(
	                                BorderFactory.createTitledBorder("The Manuscript"),
	                                BorderFactory.createEmptyBorder(5,5,5,5)),
	                areaScrollPane.getBorder()));
	        
	        //Create a text area.
	        JTextArea revArea = new JTextArea(
	                "Manuscript.getrev()"
	        );
	        revArea.setEditable(false);
	        revArea.setFont(new Font("Serif", Font.PLAIN, 14));
	        revArea.setLineWrap(true);
	        revArea.setWrapStyleWord(true);
	        JScrollPane revScrollPane = new JScrollPane(revArea);
	        revScrollPane.setVerticalScrollBarPolicy(
	                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	        revScrollPane.setPreferredSize(new Dimension(500, 500));
	        revScrollPane.setBorder(
	            BorderFactory.createCompoundBorder(
	                BorderFactory.createCompoundBorder(
	                                BorderFactory.createTitledBorder("The Reviews"),
	                                BorderFactory.createEmptyBorder(5,5,5,5)),
	                revScrollPane.getBorder()));
	 
	      
		    
		    final JButton yesButton = new JButton("Accept");
		    yesButton.addActionListener(new ActionListener() {
	        
		    	public void actionPerformed(ActionEvent e) {
		    		
		    		JOptionPane.showMessageDialog(yesButton, "Manuscript Accepted");
		    		m.setStatus(1);
//		    		System.out.println("you pushed accept"+ ", status is " + m.getStatus());
		    	}
		    });

		    final JButton noButton = new JButton("Reject");
		    noButton.addActionListener(new ActionListener() {
		        
		    	public void actionPerformed(ActionEvent e) {
		    		
		    		JOptionPane.showMessageDialog(noButton, "Manuscript Rejected");
		    		m.setStatus(-1);
//		    		System.out.println("you pushed reject"+ ", status is " + m.getStatus());
		    	}
		    });
		    
		    final JButton undecButton = new JButton("Undecided");
		    undecButton.addActionListener(new ActionListener() {
		        
		    	public void actionPerformed(ActionEvent e) {
		    		JOptionPane.showMessageDialog(undecButton, "No Action Taken");
		    		m.setStatus(0);
//		    		System.out.println("you pushed undecided" + ", status is " + m.getStatus());
		    	}
		    });
		    
		    
		    
		    JPanel selectorPanel = new JPanel();		
		    selectorPanel.setPreferredSize(new Dimension(1000,75));
	        selectorPanel.setLayout(new GridLayout(1,3)); 
	        selectorPanel.add(yesButton);
	        selectorPanel.add(noButton);
	        selectorPanel.add(undecButton);
	        

	        
	        
	        JPanel rightPane = new JPanel(new BorderLayout());
	        rightPane.add(textControlsPane, BorderLayout.PAGE_END);
	        rightPane.add(revScrollPane,
                    BorderLayout.CENTER);
	        

	        //Put everything together.
	        JPanel leftPane = new JPanel(new BorderLayout());
	        leftPane.add(textControlsPane,
	                     BorderLayout.PAGE_START);
	        leftPane.add(areaScrollPane,
	                     BorderLayout.CENTER);
	 
	        add(leftPane, BorderLayout.LINE_START);
	        add(rightPane, BorderLayout.LINE_END);
	        add(selectorPanel, BorderLayout.SOUTH);
	        
	    }
	 
	    private void addLabelTextRows(JLabel[] labels,
	                                  JTextField[] textFields,
	                                  GridBagLayout gridbag,
	                                  Container container) {
	        GridBagConstraints c = new GridBagConstraints();
	        c.anchor = GridBagConstraints.EAST;
	        int numLabels = labels.length;
	 
	        for (int i = 0; i < numLabels; i++) {
	            c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last
	            c.fill = GridBagConstraints.NONE;      //reset to default
	            c.weightx = 0.0;                       //reset to default
	            container.add(labels[i], c);
	 
	            c.gridwidth = GridBagConstraints.REMAINDER;     //end row
	            c.fill = GridBagConstraints.HORIZONTAL;
	            c.weightx = 1.0;
	            container.add(textFields[i], c);
	        }
	    }
	 

	 
	    /**
	     * Create the GUI and show it.  For thread safety,
	     * this method should be invoked from the
	     * event dispatch thread.
	     * @param m 
	     */
	    private static void createAndShowGUI(Manuscript m) {
	        //Create and set up the window.
	        JFrame frame = new JFrame("TextSamplerDemo");
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 
	        //Add content to the window.
	        frame.add(new TextSamplerDemo(m));
	 
	        //Display the window.
	        frame.pack();
	        frame.setVisible(true);
	    }
	 
	    public static void main(String[] args) {
	        //Schedule a job for the event dispatching thread:
	        //creating and showing this application's GUI.
	        SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	                 //Turn off metal's use of bold fonts
	        UIManager.put("swing.boldMetal", Boolean.FALSE);
	        
	        Manuscript m = new Manuscript(null, "The Madness of Pelagius" , null, null);
	        m.setStatus(0);
	        
	        createAndShowGUI(m);
	        
	            }
	        });
	    }
	}
